<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="潜在客户名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入潜在客户名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="潜在客户微信" prop="wechat">
        <el-input
          v-model="queryParams.wechat"
          placeholder="请输入潜在客户微信"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="潜在客户qq" prop="qq">
        <el-input
          v-model="queryParams.qq"
          placeholder="请输入潜在客户qq"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="潜在客户联系电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入潜在客户联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="录入人" prop="entrantTeacher">
        <el-input
          v-model="queryParams.entrantTeacher"
          placeholder="请输入录入人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="追踪次数" prop="followTime">-->
<!--        <el-input-->
<!--          v-model="queryParams.followTime"-->
<!--          placeholder="请输入追踪次数"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="咨询老师" prop="consultTeacher">-->
<!--        <el-input-->
<!--          v-model="queryParams.consultTeacher"-->
<!--          placeholder="请输入咨询老师"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="录入时间" prop="entryTime">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.entryTime"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择录入时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="最后追踪时间" prop="astTraceDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.astTraceDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择最后追踪时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:student:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:student:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:student:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:student:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="takeOverStudentHandle"
        >接管潜在学员</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="abandonStudentHandle"
        >放弃潜在学员</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键id" align="center" prop="id" />
      <el-table-column label="潜在客户名" align="center" prop="name" />
      <el-table-column label="潜在客户微信" align="center" prop="wechat" />
      <el-table-column label="潜在客户qq" align="center" prop="qq" />
      <el-table-column label="潜在客户联系电话" align="center" prop="phone" />
      <el-table-column label="来源：他人推荐、公众号" align="center" prop="channel" />
      <el-table-column label="录入人" align="center" prop="entrantTeacher" />
      <el-table-column label="追踪次数" align="center" prop="followTime" />
      <el-table-column label="咨询老师" align="center" prop="consultTeacher" />
      <el-table-column label="录入时间" align="center" prop="entryTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.entryTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后追踪时间" align="center" prop="astTraceDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.astTraceDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:student:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:student:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改潜在学员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="潜在客户名" prop="name">
          <el-input v-model="form.name" placeholder="请输入潜在客户名" />
        </el-form-item>
        <el-form-item label="潜在客户微信" prop="wechat">
          <el-input v-model="form.wechat" placeholder="请输入潜在客户微信" />
        </el-form-item>
        <el-form-item label="潜在客户qq" prop="qq">
          <el-input v-model="form.qq" placeholder="请输入潜在客户qq" />
        </el-form-item>
        <el-form-item label="潜在客户联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入潜在客户联系电话" />
        </el-form-item>
        <el-form-item label="录入人" prop="entrantTeacher">
          <el-input v-model="form.entrantTeacher" placeholder="请输入录入人" />
        </el-form-item>
        <el-form-item label="追踪次数" prop="followTime">
          <el-input v-model="form.followTime" placeholder="请输入追踪次数" />
        </el-form-item>
        <el-form-item label="咨询老师" prop="consultTeacher">
          <el-input v-model="form.consultTeacher" placeholder="请输入咨询老师" />
        </el-form-item>
        <el-form-item label="录入时间" prop="entryTime">
          <el-date-picker clearable
            v-model="form.entryTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择录入时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后追踪时间" prop="astTraceDate">
          <el-date-picker clearable
            v-model="form.astTraceDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后追踪时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStudent, getStudent, delStudent, addStudent, updateStudent,takeOverStudent,abandonStudent} from "@/api/system/student";
import item from "@/layout/components/Sidebar/Item.vue";

export default {
  name: "Student",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      //学员name
      name: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 潜在学员表格数据
      studentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        wechat: null,
        qq: null,
        phone: null,
        channel: null,
        entrantTeacher: null,
        followTime: null,
        consultTeacher: null,
        entryTime: null,
        astTraceDate: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询潜在学员列表 */
    getList() {
      this.loading = true;
      listStudent(this.queryParams).then(response => {
        this.studentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        wechat: null,
        qq: null,
        phone: null,
        channel: null,
        entrantTeacher: null,
        followTime: null,
        consultTeacher: null,
        entryTime: null,
        astTraceDate: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.name = selection.map(item => item.name)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加潜在学员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改潜在学员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除潜在学员编号为"' + ids + '"的数据项？').then(function() {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 接管潜在学员 */
    takeOverStudentHandle(){
      const id = this.ids;
      this.$modal.confirm('是否确认接管潜在学员:'+this.name+'？').then(() => {
        takeOverStudent(id).then(() => {
          this.getList();
          this.$modal.msgSuccess("接管成功");
          this.name = "";
        }).catch((error) => {
          console.log(error)
        });
      }).catch((error)=>{
        console.log(error)
      });
    },
    /** 放弃潜在学员 */
    /** 接管潜在学员 */
    abandonStudentHandle(){
      const id = this.ids;
      this.$modal.confirm('是否确认放弃潜在学员:'+this.name+'？').then(() => {
        abandonStudent(id).then(() => {
          this.getList();
          this.$modal.msgSuccess("放弃成功");
          this.name = "";
        }).catch((error) => {
          console.log(error)
        });
      }).catch((error)=>{
        console.log(error)
      });
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download('system/student/export', {
        ...this.queryParams
      }, `student_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
